package com.kede.app.service.impl;

import com.kede.app.domain.Virtual;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.VirtualRepository;
import com.kede.app.service.VirtualService;
import com.kede.app.service.dto.VirtualDto;
import com.kede.app.service.dto.VirtualQueryCriteria;
import com.kede.app.service.mapstruct.VirtualMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-05-11
**/
@Service
@RequiredArgsConstructor
public class VirtualServiceImpl implements VirtualService {

    private final VirtualRepository virtualRepository;
    private final VirtualMapper virtualMapper;

    @Override
    public Map<String,Object> queryAll(VirtualQueryCriteria criteria, Pageable pageable){
        Page<Virtual> page = virtualRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(virtualMapper::toDto));
    }

    @Override
    public List<VirtualDto> queryAll(VirtualQueryCriteria criteria){
        return virtualMapper.toDto(virtualRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public VirtualDto findById(String name) {
        Virtual virtual = virtualRepository.findById(name).orElseGet(Virtual::new);
        ValidationUtil.isNull(virtual.getName(),"Virtual","name",name);
        return virtualMapper.toDto(virtual);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public VirtualDto create(Virtual resources) {
        resources.setName(IdUtil.simpleUUID()); 
        return virtualMapper.toDto(virtualRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Virtual resources) {
        Virtual virtual = virtualRepository.findById(resources.getName()).orElseGet(Virtual::new);
        ValidationUtil.isNull( virtual.getName(),"Virtual","id",resources.getName());
        virtual.copy(resources);
        virtualRepository.save(virtual);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String name : ids) {
            virtualRepository.deleteById(name);
        }
    }

    @Override
    public void download(List<VirtualDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (VirtualDto virtual : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("虚拟的最小值", virtual.getMin());
            map.put("虚拟的最大值", virtual.getMax());
            map.put("执行频率  Cron", virtual.getCron());
            map.put(" createTime",  virtual.getCreateTime());
            map.put(" updateTime",  virtual.getUpdateTime());
            map.put("存放虚拟值", virtual.getValue());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}